﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HisGL.Json
{
    public class JsonHelper
    {

        /// <summary>
        /// 获取JToken的字符串值
        /// </summary>
        /// <returns></returns>
        public static string GetJTokenValue(JToken jt)
        {
            if (jt == null || jt.ToString() == "")
                return "";
            return jt.ToString();
        }

        /// <summary>
        /// DataTable 转 JArray
        /// </summary>
        /// <returns></returns>
        public static JArray ToJArray(DataTable dt)
        {
            JArray data = new JArray();
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    JObject item = new JObject();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        item.Add(dc.ColumnName, myConvert(dr[dc]));
                    }
                    data.Add(item);
                }
            }
            return data;

        }

        public static JObject ToJObject(DataRow dr)
        {
          JObject data = new JObject();
          if (dr != null)
          {
              foreach (DataColumn dc in dr.Table.Columns)
              {
                data.Add(dc.ColumnName, myConvert(dr[dc]));
              }
          }
          return data;

        }

        public static T GetJTokenValue<T>(JToken jt, T tDef)
        {
            T sResult = tDef;
            if (jt == null)
            {
                sResult = tDef;

            }else if (tDef is int)
            {
                if (jt.ToString().Trim() == "")
                  sResult = tDef;
                else
                    sResult = (T)(object)(Convert.ToInt32(jt.ToString().Trim()));
            }
            else if (tDef is double)
            {
                if (jt.ToString().Trim() == "")
                  sResult = tDef;
                else
                    sResult = (T)(object)(Convert.ToDouble(jt.ToString().Trim()));
            }
            else if (tDef is float)
            {
                if (jt.ToString().Trim() == "")
                  sResult = tDef;
                else                
                    sResult = (T)(object)(Convert.ToDouble(jt.ToString().Trim()));
            }
            else if (tDef is bool)
            {
                if (jt.ToString().Trim() == "")
                  sResult = tDef;
                else
                  sResult = (T)(object)(jt.ToString().Trim() == "1" || jt.ToString().ToLower().Trim() == "true" );
            }
            else
            {
                sResult = (T)(object)(jt.ToString().Trim());
            }
            return sResult;
        }

        private static string myConvert(object obj, string defVal = "")
        {
            if (obj == null)
                return defVal;
            else return obj.ToString();
        }

        public static string ToJson(object obj)
        {
          return JsonConvert.SerializeObject(obj);
        }

        public static T JsonTo<T>(string json)
        {
          return JsonConvert.DeserializeObject<T>(json);
        }

    }
}
